package com.wanxi.service.impl;

import com.wanxi.dao.UserDao;
import com.wanxi.dao.impl.UserImpl;
import com.wanxi.model.ResultModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.util.Md5;
import sun.security.provider.MD2;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    @Override
    public ResultModel enable(UserModel model) {
        return ResultModel.getResult(userDao.enable(model));
    }

    @Override
    public ResultModel add(UserModel model) {
        UserDao userDao = new UserImpl();
        String yuanmima = model.getPassword();
        String jiamimima = Md5.encoderByMd5(yuanmima);
//        调用dao层之前，需要对密码进行加密
        model.setPassword(jiamimima);
//        ResultModel resultModel = new ResultModel();
//        resultModel.setCount(userDao.add(model));
//        return resultModel;
        return ResultModel.getResult(userDao.add(model));
    }

    @Override
    public ResultModel update(UserModel model) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setCount(userDao.update(model));
//        return resultModel;
        return ResultModel.getResult(userDao.update(model));
    }

    @Override
    public ResultModel delete(UserModel model) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setCount(userDao.delete(model));
//        return resultModel;
        return ResultModel.getResult(userDao.delete(model));
    }

    @Override
    public ResultModel findById(UserModel model) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setData(userDao.findById(model));
//        return resultModel;
        return ResultModel.getResult(userDao.findById(model));
    }

    @Override
    public ResultModel findAll(UserModel model) {
        return ResultModel.getResult(userDao.findAll(model), userDao.count(model));
    }

    @Override
    public ResultModel count(UserModel model) {
        return ResultModel.getResult(userDao.count(model));
    }

    @Override
    public ResultModel login(UserModel userModel) {
        String yuanmima = userModel.getPassword();
        String jiamimima = Md5.encoderByMd5(yuanmima);
//        调用dao层之前，需要对密码进行加密
        userModel.setPassword(jiamimima);
        UserModel model = userDao.findByUsernameAndPassword(userModel);
        if (model == null) {
            return ResultModel.error();
        } else {
            return ResultModel.success();
        }

    }

    @Override
    public ResultModel password(UserModel model) {
        /**
         * 1.首先判断旧密码是否一致
         * 2.如果一致则修改新密码
         * 3.如果不一致，则提示返回影响的条数为-1
         */
        model.setNewPassword(Md5.encoderByMd5(model.getNewPassword()));
        model.setPassword(Md5.encoderByMd5(model.getPassword()));
//        通过id和旧密码去数据库查询是否有这条数据
//        UserModel userModel=userDao.findById(model);
//        if(userModel.getPassword().equals(model.getOldPassword())){
//
//        }
//        以后不能将密码查询出来和其他进行比较
        // 这里是用的以前登录的代码
        UserModel userModel = userDao.findByUsernameAndPassword(model);
        if (userModel == null) {
            return ResultModel.getResult(-1);
        } else {
            return ResultModel.getResult(userDao.updatePassword(model));
        }

    }
}
